﻿using PoemGame.ApplicationService;
using PoemGame.Domain.Services;
using PoemGame.Domain.Services.Factory;
using GamePlayServiceFactory = PoemGame.ApplicationService.GamePlayServiceFactory;

namespace PoemGame.SignalR
{
    public static class DomainServiceExtension
    {
        public static IServiceCollection AddPoemGameDomainService(this IServiceCollection services, IConfiguration configuration)
        {
            var poemGameServices = configuration.GetSection("PoemGameServices").Get<string[]>();
            if (poemGameServices == null)
            {
                poemGameServices = "PoemGame.Domain.Services.Feihualing,PoemGame.Domain.Services.Duishi,PoemGame.Domain.Services.Jielong".Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            }
            AppDomain currentDomain = AppDomain.CurrentDomain;
            //var arr=poemGameServices.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in poemGameServices)
            {
                currentDomain.Load(item);
            }

            var scanners = AppDomain.CurrentDomain.GetAssemblies().ToList()
                    .SelectMany(x => x.GetTypes())
                    .Where(t => t.GetInterfaces().Contains(typeof(IDomainService)) && t.IsClass).ToList();

            foreach (Type type in scanners)
            {
                services.AddScoped(type);
            }
            services.AddScoped<IDomainServiceFactory<ICheckAnswerService>, DomainServiceFactory<ICheckAnswerService>>();
            services.AddScoped<IDomainServiceFactory<ICheckGameConditionService>, DomainServiceFactory<ICheckGameConditionService>>();
            services.AddScoped<IGameFactory, GameFactory>();
            services.AddScoped<GamePlayInturnService>();
            services.AddScoped<GamePlayFCFAService>();
            services.AddScoped<IGamePlayServiceFactory, GamePlayServiceFactory>();
            return services;
        }
    }
}
